<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>手写Promise</title>
</head>

<body>
  <h2>then方法-异步及多次调用</h2>
  <script>
    /**
     * then方法-异步及多次调用
     *  1. 定义实例属性
     *  2. 保存回调函数
     *  3. 调用成功回调
     *  4. 调用失败回调
     * */
    const PENDING = 'pending'
    const FULFILLED = 'fulfilled'
    const REJECTED = 'rejected'
    class HMPromise {
      // 状态
      state = PENDING
      // 原因
      result = undefined
      // 1. 定义实例属性
      #handlers = [] // [{onFulfilled,onRejected}...]

      // 构造函数
      constructor(func) {
        // pending->fulfilled
        const resolve = (result) => {
          if (this.state === PENDING) {
            this.state = FULFILLED
            this.result = result
            // 3. 调用成功回调
            this.#handlers.forEach(({ onFulfilled }) => {
              onFulfilled(this.result)
            })
          }
        }
        // pending->rejected
        const reject = (result) => {
          if (this.state === PENDING) {
            this.state = REJECTED
            this.result = result
            // 4. 调用失败回调
            this.#handlers.forEach(({ onRejected }) => {
              onRejected(this.result)
            })
          }
        }
        func(resolve, reject)
      }

      // then方法
      then(onFulfilled, onRejected) {
        onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : x => x
        onRejected = typeof onRejected === 'function' ? onRejected : x => { throw x }

        if (this.state === FULFILLED) {
          onFulfilled(this.result)
        } else if (this.state === REJECTED) {
          onRejected(this.result)
        } else if (this.state === PENDING) {
          // 2. 保存回调函数
          this.#handlers.push({
            onFulfilled, onRejected
          })
        }
      }
    }

    // ------------- 测试代码 -------------
    const p = new HMPromise((resolve, reject) => {
      setTimeout(() => {
        resolve('success')
        // reject('error')
      }, 2000);
    })

    p.then(res => {
      console.log('then1:', res)
    }, err => {
      console.log('then1:', err)
    })

    p.then(res => {
      console.log('then2:', res)
    }, err => {
      console.log('then2:', err)
    })

  </script>
</body>

</html>